/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.financeiro.filter;

import br.jm.financeiro.base.eao.EaoException;
import br.jm.financeiro.entity.Proposta;
import br.jm.financeiro.sistema.eao.PropostaEao;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

/**
 *
 * @author joao
 */
public class PropostaConverter implements Converter {

    public static List<Proposta> propostaList;
    private PropostaEao propostaEAO;
    private Proposta proposta = new Proposta();
    

    public PropostaConverter() throws EaoException {
        propostaEAO = PropostaEao.getInstance();
        propostaList = propostaEAO.getNative("Proposta", 0, propostaEAO.getCount("Proposta"), "tipStatus", true, null, null, null);
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {

        if (value.trim().equals("")) {
            return null;
        } else {
            try {
                int number = Integer.parseInt(value);

                proposta = propostaEAO.getPorId(number);
                propostaList.add(proposta);

                return proposta;
            } catch (NumberFormatException | EaoException exception) {
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid Proposta"));
            }
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null || value.equals("")) {
            return "";
        } else {
            return String.valueOf(((Proposta) value).getIdProposta());
        }
    }
}
